<?php

require_once ("AWSSDKforPHP/sdk.class.php");
require_once ("AWSSDKforPHP/extensions/s3streamwrapper.class.php");
require_once ("Console/ProgressBar.php");

// Heredoc arbitrary expression hack declaration
function _expr($v) { return $v; }
$_expr = '_expr';
/* Usage
     echo "pre {$_expr(1+2)} post\n"; // outputs 'pre 3 post'
     echo "pre {$_expr(qwe)} post\n"; // outputs 'pre asd post'
     echo "pre {$_expr(c($a, $b)+zxc*2)} post\n"; // outputs 'pre 17 post'
*/

// Class Autoload Function
function __autoload($className) {
	$fqpn = realpath(dirname(__FILE__));
	$classFQPN = __searchForClass($fqpn, $className);	
	
	if ($classFQPN) {
		require_once($classFQPN);
		return true;
	}
}

// Search the class dir (and sub-dirs) for the class
function __searchForClass($basedir, $className) {
	$dir = dir($basedir);
	while (false !== ($entry = $dir->read())) {
		if ($entry == "." || $entry == "..") {
			continue;
		}
		$fqpn = "$basedir/$entry";
		if (is_dir($fqpn)) {
			$classPath = __searchForClass($fqpn, $className);
			if ($classPath) {
				return $classPath;
			}
		}

		if ("$className.php" == $entry) {
			if (file_exists($fqpn)) {
				return $fqpn;
			}
		}
	}
	return false;
}

/*
// General error handler
function __ErrorHandler($errno = null, $errstr = null, $errfile = null, $errline = null) {
	// if no errno was set, register_shutdown_function must have called 
	if (is_null($errno)) {
		$errorData = error_get_last();
		if (isset($errorData["type"])) {
			$errno = $errorData["type"];
		}
		if (isset($errorData["message"])) {
			$errstr = $errorData["message"];
		}
		if (isset($errorData["file"])) {
			$errfile = $errorData["file"];
		}
		if (isset($errorData["line"])) {
			$errline = $errorData["line"];
		}
	}
	
	// if errno is still null, the script has ended and/or no error exists
	if (is_null($errno)) {
		return true;
	}
	$config = new Config();
	$debug = new Debug(Debug::DEBUG_ENABLE | Debug::DEBUG_ERRS);
	$debug->out("errno ($errno) errstr ($errstr) errfile ($errfile) errline ($errline)");
	return true;
}
// Register the error handler function
set_error_handler("__ErrorHandler");
register_shutdown_function("__ErrorHandler");

*/

// Register the Autoloader function
spl_autoload_register("__autoload");

?>
